<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{margin: 0;padding: 0;}
        div{
            width: 200px;
            height: 200px;
            margin: 0 auto;
            position: relative;
            text-align: center;
        }
        input{
            width: 100px;
            height: 30px;
            text-align: center;
            position: relative;
        }
        ul{
            display: none;
            position: absolute;
            list-style: none;
            height: 100px;
            width: 102px;
            top: 34px;
            left: 49px;
        }
        ul li{
            height: 20%;
            width: 100%;
            text-align: center;
        }
    </style>
</head>
<body>
    <div>
        <input type="text" id="text">
        <ul id="ul">
            <li>北京</li>
            <li>上海</li>
            <li>东莞</li>
            <li>重庆</li>
            <li>成都</li>
        </ul>
    </div>
    
    <script>
        var ul1 = document.querySelector('#ul')
        var text = document.querySelector('#text')
        var list = document.querySelectorAll('ul li')
        console.log(list)
        text.onfocus = function(){
            ul1.style.display = 'block'
        }
        text.onclick = function(e){
            e.stopPropagation()
        }
        
        document.onclick = function(){
            ul1.style.display = 'none'
        }
        // text.onblur = function(e){
        //     ul1.style.display = 'none'
        // }
        for(var i=0;i<list.length;i++){
            // list[i].onmousedown = function(e){
            //     if(e.buttons ===1 ){
            //         text.value = this.innerHTML
            //     } 
            // }
            list[i].onclick = function(e){
                text.value = this.innerHTML
            }
        }
    </script>
</body>
</html>